package com.lie.flat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lie.flat.constant.CommonConst;
import com.lie.flat.entity.Card;
import com.lie.flat.entity.CardRequest;
import com.lie.flat.service.CardResponseService;
import com.lie.flat.service.connect.CardCacheService;
import com.lie.flat.service.connect.ConnectService;
import com.lie.flat.util.CommonReturn;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.List;

@RestController
@RequestMapping("/cardInfo")
@Slf4j
public class CardResponseController {
    @Autowired
    @Qualifier(value = "json")
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private CardResponseService cardResponseService;
    @Autowired
    private ConnectService connectService;

    @Autowired
    private CardCacheService cardCacheService;


    /**
     * 向第三方接口发送请求，获取卡列表信息并更新存入数据库
     *
     * @param cardRequest
     * @param cardRequest
     * @return
     */
    @PostMapping("/cardReq")
    @CrossOrigin
    public CommonReturn cardReq(@RequestBody CardRequest cardRequest) {
        //查询数据库中卡信息的sqlCount
        String str = RandomStringUtils.randomAlphanumeric(6);
        cardRequest.setSysId("addd");
        cardRequest.setPasswd("2wsx#EDC");
        cardRequest.setSerialNo(str);


        QueryWrapper<Card> cardQueryWrapper = new QueryWrapper<>();
        //redis 跟踪数据库卡的总数
        Integer sqlCount = (Integer) redisTemplate.opsForValue().get(CommonConst.RDS_KEY_CARDS_CNT);
        if (sqlCount == null || sqlCount == 0) {
            int cardsInDB = cardResponseService.count(cardQueryWrapper);
            redisTemplate.opsForValue().setIfAbsent(CommonConst.RDS_KEY_CARDS_CNT, cardsInDB, Duration.ofSeconds(180));
            sqlCount = (Integer) redisTemplate.opsForValue().get(CommonConst.RDS_KEY_CARDS_CNT);
        }

        //从第三方接口接受卡信息数据，并计算webCount
        List<Card> list = null;
        try {
            list = cardCacheService.getCard(cardRequest);
        } catch (Exception e) {
            log.error("+++++++++++++获取第三方卡信息失败++++++++++");
            return CommonReturn.error(e.getMessage());
        }
        if (list == null) {
            return CommonReturn.success(cardResponseService.list(cardQueryWrapper));
        }
        int webCount = list.size();

        //验证第三方接口传入的卡信息是否与数据库的相等，如果相等，则跳过查询插入数据库过程，如果不匹配，则更新数据库
        if (sqlCount == null || sqlCount == 0 || sqlCount != webCount) {

            cardResponseService.saveOrUpdateBatch(list);
            redisTemplate.opsForValue().setIfAbsent(CommonConst.RDS_KEY_CARDS_CNT, webCount, Duration.ofSeconds(180));
        }

        return CommonReturn.success(list);
    }
/**
 @GetMapping("/card")
 @CrossOrigin public CommonReturn<Page> page(int page, int pageSize) {

 Page pageInfo = new Page(page, pageSize);
 pageInfo.setTotal(pageInfo.getRecords().size());
 LambdaQueryWrapper<Card> cardLambdaQueryWrapper = new LambdaQueryWrapper<>();
 cardLambdaQueryWrapper.orderByDesc(Card::getCardNo);
 cardResponseService.page(pageInfo, cardLambdaQueryWrapper);

 return CommonReturn.success(pageInfo);
 }
 */
}
